import * as THREE from 'three';

export function getFakeData(total: number, max: number) {
    let aim = [];
    for (let i = 0; i < total; i++) {
        let t = getUniqueIntegers(0, max);
        let item = {
            id: i,
            in: "0x" + t[0],
            out: "0x" + t[1]
        }
        aim.push(item);
    }
    return aim;
}

export function getUniqueIntegers(a: number, b: number) {
    const min = Math.ceil(Math.min(a, b));
    const max = Math.floor(Math.max(a, b));

    const uniqueIntegers = new Set();
    while (uniqueIntegers.size < 2) {
        const randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
        uniqueIntegers.add(randomInt);
    }

    return Array.from(uniqueIntegers);
}

export function getAddress(list: any[]) {
    let aim: any[] = [];
    list.forEach(item => {
        if (aim.includes(item.in) == false) {
            aim.push(item.in)
        }
        if (aim.includes(item.out) == false) {
            aim.push(item.out)
        }
    })
    return aim;
}

export function getLines(list: any[]) {
    let aim: any[] = [];
    list.forEach(item => {
        aim.push({
            id: item.id,
            to: item.in,
            from: item.out
        })
    })
    return aim;
}

export function getRandomInt(a: number, b: number) {
    const min = Math.ceil(Math.min(a, b));
    const max = Math.floor(Math.max(a, b));
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

export function worldToScreen(obj: THREE.Object3D, camera: THREE.Camera) {
    const vector = new THREE.Vector3();
    vector.setFromMatrixPosition(obj.matrixWorld); // 将 Mesh 的 3D 位置转换为世界坐标系
    vector.project(camera); // 投影到屏幕坐标系
    return vector;
}

export function sleep(timer:number){
    return new Promise(resolve=>{
        setTimeout(resolve, timer)
    })
}


export function onDatas(blob: any) {
    const pako = (window as any).pako;
    return new Promise(resolve => {
        if (typeof blob == "string") {
            resolve(blob);
            return;
        }
        // console.log(blob);
        // 创建一个FileReader对象
        let reader = new FileReader();

        // 读取Blob对象
        reader.onload = function () {
            // 获取Blob对象中的数据
            let data: any = reader.result;
            // window.wsdata = data;
            // 将数据转换为字符串
            let str = String.fromCharCode.apply(null, new Uint8Array(data));
            // 将base64编码的数据解码为二进制数据
            const binaryData = atob(str);

            // 将二进制数据转换为Uint8Array
            const uint8Array = new Uint8Array(binaryData.length);
            for (let i = 0; i < binaryData.length; i++) {
                uint8Array[i] = binaryData.charCodeAt(i);
            }

            // 解压缩gzip数据
            const decompressedData = pako.inflate(uint8Array, { to: "string" });

            // console.log(decompressedData); // 输出解压缩后的数据
            resolve(decompressedData);

            // //var wsdata = pako.ungzip(str, {to: \"string\"});
            // // 解析为JSON对象
            // //let json = JSON.parse(str);

            // console.log(str); // 输出解析后的JSON对象
        };
        //var wsdata = pako.ungzip(ev.data, {to: \"string\"});
        reader.readAsArrayBuffer(blob);
        //console.warn(ev)
        // if (!_this.recvPause) {
        // 	var data = ev.data;

        // 	if (_this.recvClean) _this.messageData = [];
        // 	_this.writeNews(0, data);
        // }
    })

}

export function getRect(points: any[]) {

    let minX = Number.MAX_VALUE;
    let maxX = Number.MIN_VALUE;
    let minY = Number.MAX_VALUE;
    let maxY = Number.MIN_VALUE;

    // 遍历点列表，找到最小和最大的x、y值
    for (let i = 0; i < points.length; i++) {
        const point = points[i];
        minX = Math.min(minX, point.x);
        maxX = Math.max(maxX, point.x);
        minY = Math.min(minY, point.y);
        maxY = Math.max(maxY, point.y);
    }

    const width = maxX - minX;
    const height = maxY - minY;

    return {
        left: minX,
        right: maxX,
        top: maxY,
        bottom: minY,
        width: width,
        height: height
    };

}